package fanshe;

import java.io.IOException;
import java.lang.reflect.Constructor;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
import javassist.NotFoundException;

//测试使用javaassist生成一个新的类
public class demo05 {
	public static void main(String[] args) throws CannotCompileException, NotFoundException, IOException {
			ClassPool pool=ClassPool.getDefault();
			CtClass cc=pool.makeClass("fanshe.Emp");
			CtField f=CtField.make("private int empno;", cc);
			CtField f1=CtField.make("private String ename;", cc);
			cc.addField(f);
			cc.addField(f1);
			CtMethod m=CtMethod.make("public int getEmpno(){return empno;}", cc);
			CtMethod m1=CtMethod.make("public void setEmpno(int empno){this.empno=empno;}", cc);
			cc.addMethod(m);
			cc.addMethod(m1);
			CtConstructor con=new CtConstructor(new CtClass[] {CtClass.intType,pool.get("java.lang.String")} ,cc);
			con.setBody("{this.empno=empno;this.ename=ename;}");
			cc.writeFile("d:/ok");
	}
}
